package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import business.BusinessComment;
import business.BusinessScoredSeen;
import business.BusinessUser;
import domain.ScoredSeen;
import domain.User;

@WebServlet(description = "Servlet para Video", urlPatterns = { "/VideoController" })
public class VideoController extends HttpServlet {
		
	private static final long serialVersionUID = 1L;
	      
	    /**
	     * @see HttpServlet#HttpServlet()
	     */
	    public VideoController() {
	        super();
	        // TODO Auto-generated constructor stub
	    }

		/**
		 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
		 */
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			if(request.getParameter("visto")!=null){
				modificarVisto(request, response);
			}else if(request.getParameter("puntuar")!=null){
				modificarPuntuacion(request,response);
			}else if(request.getParameter("comentar")!=null){
				crearComentario(request,response);
			}else if(request.getParameter("anadir")!=null){
				anadirVideo(request, response);
			}
		}

		/**
		 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
		 */
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			doGet(request, response);
		}
		
		public void modificarVisto(HttpServletRequest request,
				HttpServletResponse response) throws IOException, ServletException {
			HttpSession session = request.getSession();
			User user=(User)session.getAttribute("session.user");
			Integer idVideo=(Integer)session.getAttribute("session.idVideo");
			session.removeAttribute("session.idVideo");
			Integer visto=1;
			if(request.getParameter("visto").equals("Vista")){
				visto=0;
			}
			ScoredSeen scoredSeen = new ScoredSeen(idVideo, user.getIdUser(), 0, 0.0, visto);
			BusinessScoredSeen.changeSeen(scoredSeen);
			session.setAttribute("session.user", BusinessUser.getUserById(user.getIdUser()));
			if(request.getParameter("tipoVideo").equals("Serie")){
				response.sendRedirect("serie.jsp?idVideo="+idVideo);
			}else{
				response.sendRedirect("movie.jsp?idVideo="+idVideo);
			}
		}
		
		public void modificarPuntuacion(HttpServletRequest request,
				HttpServletResponse response) throws IOException, ServletException {
			HttpSession session = request.getSession();
			User user=(User)session.getAttribute("session.user");
			Integer idVideo=(Integer)session.getAttribute("session.idVideo");
			session.removeAttribute("session.idVideo");
			Integer puntos= new Integer(request.getParameter("puntos"));
			ScoredSeen scoredSeen = new ScoredSeen(idVideo, user.getIdUser(), puntos, null, null);
			BusinessScoredSeen.changeScore(scoredSeen);
			session.setAttribute("session.user", BusinessUser.getUserById(user.getIdUser()));
			if(request.getParameter("tipoVideo").equals("Serie")){
				response.sendRedirect("serie.jsp?idVideo="+idVideo);
			}else{
				response.sendRedirect("movie.jsp?idVideo="+idVideo);
			}
		}
		
		public void crearComentario(HttpServletRequest request,
				HttpServletResponse response) throws IOException, ServletException {
			HttpSession session = request.getSession();
			User user=(User)session.getAttribute("session.user");
			Integer idVideo=(Integer)session.getAttribute("session.idVideo");
			session.removeAttribute("session.idVideo");
			String comentario= (String)request.getParameter("comentario");
			if(comentario!=null){
				BusinessComment.insertComment(idVideo, user.getIdUser(), comentario);
			}
			session.setAttribute("session.user", BusinessUser.getUserById(user.getIdUser()));
			if(request.getParameter("tipoVideo").equals("Serie")){
				response.sendRedirect("serie.jsp?idVideo="+idVideo);
			}else{
				response.sendRedirect("movie.jsp?idVideo="+idVideo);
			}
		}
		
		public void anadirVideo(HttpServletRequest request,
				HttpServletResponse response) throws IOException, ServletException {
			HttpSession session = request.getSession();
			User user=(User)session.getAttribute("session.user");
			Integer idVideo=(Integer)session.getAttribute("session.idVideo");
			session.removeAttribute("session.idVideo");
			BusinessScoredSeen.insertVideoUsuario(user.getIdUser(), idVideo);
			session.setAttribute("session.user", BusinessUser.getUserById(user.getIdUser()));
			if(request.getParameter("tipoVideo").equals("Serie")){
				response.sendRedirect("serie.jsp?idVideo="+idVideo);
			}else{
				response.sendRedirect("movie.jsp?idVideo="+idVideo);
			}
		}

	}